"""
pickle 是 Python 的一个内置模块，用于实现对象的序列化和反序列化
- 序列化：使用 pickle.dump() 或 pickle.dumps() 方法。
- 反序列化：使用 pickle.load() 或 pickle.loads() 方法
"""
import pickle

# 1. 序列号字典
data = {
    'a': [1, 2, 3],
    'b': 'gavin',
    (1, 2): [1, 2]
}

with open('test.pkl', 'wb') as f:
    pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)

with open('test.pkl', 'rb') as f:
    data = pickle.load(f)
    print(data)


# 2. 序列号对象
class Student:
    def __init__(self, name):
        self.name = name


student_1 = Student('Gavin')

with open('test2.pkl', 'wb') as f:
    pickle.dump(student_1, f)

with open('test2.pkl', 'rb') as f:
    student = pickle.load(f)
    print(student.name)
